package com.woniu.smallmedical.server.serverimpl;

import com.woniu.smallmedical.entity.*;
import com.woniu.smallmedical.mapper.AddressMapper;
import com.woniu.smallmedical.mapper.PermissionMapper;
import com.woniu.smallmedical.server.AddressServer;
import com.woniu.smallmedical.server.PermissionServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author author
 * @create 2021-09-26-8:59
 */
@Service
public class PermissionServerImpl implements PermissionServer {

    @Autowired
    PermissionMapper permissionMapper;

    @Override
    public List<Role> getRole() {
        List<Role> roles = permissionMapper.selectRole();
        return roles;
    }

    @Override
    public List<TreeMenu> getTreeMenu() {
        List<TreeMenu> treeMenus = permissionMapper.selectTreeMenu();
        return treeMenus;
    }

    @Override
    public Role getTreeMenuByRid(Integer rid) {
        Role role = permissionMapper.selectTreeMenuByRid(rid);
        return role;
    }

    @Override
    public TreeMenu getTreeMenuByMid(Integer mid) {
        TreeMenu treeMenu = permissionMapper.selectTreeMenuByMid(mid);
        return treeMenu;
    }

    @Override
    public TreeMenu getTreeMenuByMpid(Integer mpid) {
        TreeMenu treeMenu = permissionMapper.selectTreeMenuByPmid(mpid);
        return treeMenu;
    }
    @Transactional
    @Override
    public void updateMenuPermission(Integer rid, Integer[] mid,Integer[] pid) {
        /*新增按钮权限*/
        Set<RolePermission> rolePermissionsSet=new HashSet<>();
        for(Integer i:pid)
        {
            Permission permissionByPid = getPermissionByPid(i);
            RolePermission rolePermission=new RolePermission();
            rolePermission.setRid(rid);
            rolePermission.setPid(permissionByPid.getPid());
            rolePermissionsSet.add(rolePermission);
            if(permissionByPid.getPpid()!=0){
                Permission permissionByPpid = getPermissionByPpid(permissionByPid.getPpid());
                RolePermission rolePermissionParent=new RolePermission();
                rolePermissionParent.setRid(rid);
                rolePermissionParent.setPid(permissionByPid.getPid());
                rolePermissionsSet.add(rolePermission);
            }
        }

        /*新增菜单权限*/
        Set<RoleMenu> treeMenu =new HashSet<>();
        for(Integer i:mid)
        {

            TreeMenu treeMenuByMid = getTreeMenuByMid(i);
            RoleMenu roleMenu=new RoleMenu();
            roleMenu.setRid(rid);
            roleMenu.setMid(treeMenuByMid.getMid());
            roleMenu.setPmid(treeMenuByMid.getMpid());
            treeMenu.add(roleMenu);
            if(treeMenuByMid.getMpid()!=0){
                TreeMenu treeMenuByMpid = getTreeMenuByMpid(treeMenuByMid.getMpid());
                RoleMenu roleMenuParent=new RoleMenu();
                roleMenuParent.setRid(rid);
                roleMenuParent.setMid(treeMenuByMpid.getMid());
                roleMenuParent.setPmid(treeMenuByMpid.getMpid());
                treeMenu.add(roleMenuParent);
            }
        }
        deleteMenuPermission(rid);
        for(RoleMenu roleMenu:treeMenu){

            insertMenuPermission(roleMenu);
        }
        deleteRolePermission(rid);
        for(RolePermission rolePermission:rolePermissionsSet){
            System.out.println(rolePermission);
            insertRolePermissio(rolePermission);
        }


    }

    @Override
    public void deleteMenuPermission(Integer rid) {
        permissionMapper.deleteRoleMenuByRid(rid);
    }

    @Override
    public void insertMenuPermission(RoleMenu roleMenu) {
        permissionMapper.insertRoleMenu(roleMenu);
    }

    @Override
    public List<Permission> getPermission() {
        List<Permission> permissions = permissionMapper.selectPermission();
        return permissions;
    }

    @Override
    public Permission getPermissionByPid(Integer pid) {
        Permission permission = permissionMapper.selectPermissionByPid(pid);
        return permission;
    }

    @Override
    public Permission getPermissionByPpid(Integer ppid) {
        Permission permission = permissionMapper.selectPermissionByPpid(ppid);
        return permission;
    }

    @Override
    public void deleteRolePermission(Integer rid) {
        permissionMapper.deleteRolePermissionByRid(rid);
    }

    @Override
    public void insertRolePermissio(RolePermission rolePermission) {
        permissionMapper.insertRolePermission(rolePermission);
    }


}
